<script setup>
import {pageofficeOpenFile} from "@/plugins/pageoffice/pageoffice-common.js";
import {backMyDocList} from "@/utils/backList.js";
import {applyJointSign, jointSignComplete} from "@/api/document/myDocument.js";
import SelectUserList from "@/views/common/selectUserList.vue";
import AdviceList from "@/views/document/common/advice/adviceList.vue";
import modal from "@/plugins/modal.js";
import {filemakerctrl} from "js-pageoffice";
import {getApplyUserPageList} from "@/api/contract/myContract.js";
import CancelButton from "@/views/document/my/flowInfo/common/cancelButton.vue";
import {setCurrentIsRefreshAdvice} from "@/utils/document.js";

const {proxy} = getCurrentInstance()
const selectUserListRef = ref(null)
const loading = ref(false)
const emits = defineEmits(['loadMainViewData'])
const prop = defineProps({
  docId: {
    type: Number
  },
  flowInfoButtons: {
    type: JSON
  },
  isRefreshAdvice: {
    type: Boolean
  }
})
const currentIsRefreshAdvice = ref(prop.isRefreshAdvice)

function openApprovalFile() {
  pageofficeOpenFile('/document/my/common/openApprovalFile/' + prop.docId, prop.docId)
}

function openMainFile() {
  pageofficeOpenFile('/document/my/common/openMainFile/' + prop.docId, prop.docId)
}

function refreshMainViewData() {
  loading.value = false
  emits("loadMainViewData")
}

function handleNext() {
  modal.confirm('确认已会签完毕，进行领导签发？').then(() => {
    modal.loading('正在执行，请稍后...')
    return handleApprovalAutoFill(prop.docId)
  })
}

//进入下一步前呈批表自动填充，处理没有填写审批意见时相关的数据区域中的多余文本
function handleApprovalAutoFill(docId) {
  filemakerctrl.SaveFilePage = '/doc/my/updateApprovalWord/' + docId
  filemakerctrl.CallFileMaker({
    url: "/doc/my/nextApprovalAutoFill/" + docId,
    success: function () {
      jointSignComplete(docId).then(() => {
        modal.closeLoading()
        refreshMainViewData()
      }).catch((msg) => {
        modal.closeLoading()
        proxy.$modal.alertError(msg)
      })
    },
    error: function (msg) {
      modal.closeLoading()
      proxy.$modal.alertError(msg)
    }
  })
}

function handleApply() {
  selectUserListRef.value.openDialog()
}

function getApplyUserList(queryParams, callback) {
  getApplyUserPageList(queryParams).then(res => {
    callback(res)
  })
}

function handleSelectedUser(user) {
  loading.value = true
  const query = {id: prop.docId, userId: user.id}
  applyJointSign(query).then(() => {
    setCurrentIsRefreshAdvice(currentIsRefreshAdvice)
    proxy.$modal.msgSuccess("执行成功")
    refreshMainViewData()
  }).catch((error) => {
    loading.value = false
    proxy.$modal.alertError(error)
  })
}

</script>

<template>
  <el-row v-loading="loading" element-loading-text="正在执行，请稍后...">
    <el-col :span="12" class="left">
      <el-row>
        <el-form-item>
          <el-button @click="backMyDocList">返 回</el-button>
          <el-button type="primary" @click="openApprovalFile">打开呈批表</el-button>
          <el-button type="primary" @click="openMainFile">打开正文</el-button>
          <el-button type="primary" @click="handleApply" v-if="flowInfoButtons.isApply">申请会签</el-button>
          <el-button type="success" @click="handleNext" v-if="flowInfoButtons.isNext">会签完毕</el-button>
        </el-form-item>
      </el-row>
    </el-col>
    <el-col :span="12">
      <el-row justify="end">
        <el-form-item>
          <CancelButton :docId="docId" :isCancel="flowInfoButtons.isCancel"/>
        </el-form-item>
      </el-row>
    </el-col>
  </el-row>
  <!-- 选择用户对话框 -->
  <SelectUserList ref="selectUserListRef" @getApplyUserList="getApplyUserList" @handleSelectedUser="handleSelectedUser"/>
  <!-- 意见列表 -->
  <AdviceList :isRefreshAdvice="currentIsRefreshAdvice"/>
</template>

